package com.zka.crm.settings.web.controller;

import com.zka.crm.common.Constant.Constant;
import com.zka.crm.common.domain.ReturnObject;
import com.zka.crm.common.utils.DateUtils;
import com.zka.crm.settings.domain.User;
import com.zka.crm.settings.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
public class UserController {
    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping("/settings/qx/user/toLogin.do")
    public String toLogin(){
        System.out.println("to login...");
        return "settings/qx/user/login";
    }

    @RequestMapping("/settings/qx/user/login.do")
    public @ResponseBody Object login(String login_act, String pwd, String isRemPwd,
                                      HttpServletRequest request, HttpSession session,
                                      HttpServletResponse response){
        System.out.println("login check...");
        Map<String, Object> map = new HashMap<>();
        map.put("loginAct_fromReq",login_act);
        map.put("loginPwd_fromReq",pwd);
        User user = userService.queryUserByLoginActAndPwd(map);

        ReturnObject returnObject = new ReturnObject();
//        System.out.println(user.getAllowips());
//        System.out.println(request.getRemoteAddr());
        if(user==null) {
            //login failed
            System.out.println("no user");
            returnObject.setCode(Constant.RETURN_OBJECT_CODE_FAILED);
            returnObject.setMessage("用户名或者密码错误！");
        }else{
            //judge if the user is valid
            String nowdate = DateUtils.date_formate(new Date());
            if(nowdate.compareTo(user.getExpiretime())>0){
                returnObject.setCode(Constant.RETURN_OBJECT_CODE_FAILED);
                returnObject.setMessage("账号过期");
            }else if("0".equals(user.getLockstate())){//是否锁定
                returnObject.setCode(Constant.RETURN_OBJECT_CODE_FAILED);
                returnObject.setMessage("账号被锁定");
            }else if(!user.getAllowips().contains(request.getRemoteAddr())){//判断ip是否安全
                returnObject.setCode(Constant.RETURN_OBJECT_CODE_FAILED);
                returnObject.setMessage("非常用ip");
            }else{
                //登录成功
                returnObject.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);
                returnObject.setMessage("登录成功！");
                session.setAttribute(Constant.SESSION_USER,user);
                //记住密码
                if("true".equals(isRemPwd)){
                    Cookie cookie = new Cookie("loginAct", user.getLoginact());
                    cookie.setMaxAge(10*24*60*60);
                    response.addCookie(cookie);
                    Cookie cookie1 = new Cookie("loginPwd", user.getLoginpwd());
                    cookie1.setMaxAge(10*24*60*60);
                    response.addCookie(cookie1);
                }else{
                    //删除cookie
                    Cookie c1 = new Cookie("loginAct","1");
                    c1.setMaxAge(0);
                    response.addCookie(c1);
                    Cookie c2 = new Cookie("loginPwd","1");
                    c2.setMaxAge(0);
                    response.addCookie(c2);
                }
            }
        }
        return returnObject;
    }

    @RequestMapping("/settings/qx/user/logout.do")
    public String logout(HttpSession session, HttpServletResponse response){
        System.out.println("logout");
        //删除cookie
        Cookie c1 = new Cookie("loginAct","1");
        c1.setMaxAge(0);
        response.addCookie(c1);
        Cookie c2 = new Cookie("loginPwd","1");
        c2.setMaxAge(0);
        //session
        session.invalidate();
        return "redirect:/";
//        return "settings/qx/user/login";
    }
}
